package org.dromara.hm.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.hm.domain.bo.HmCustomerTransferHistoryBo;
import org.dromara.hm.domain.vo.HmCustomerTransferHistoryVo;
import org.dromara.hm.domain.HmCustomerTransferHistory;
import org.dromara.hm.mapper.HmCustomerTransferHistoryMapper;
import org.dromara.hm.service.IHmCustomerTransferHistoryService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 客户迁移历史Service业务层处理
 *
 * @author Hzy
 * @date 2025-05-23
 */
@RequiredArgsConstructor
@Service
public class HmCustomerTransferHistoryServiceImpl implements IHmCustomerTransferHistoryService {

    private final HmCustomerTransferHistoryMapper baseMapper;

    /**
     * 查询客户迁移历史
     *
     * @param id 主键
     * @return 客户迁移历史
     */
    @Override
    public HmCustomerTransferHistoryVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询客户迁移历史列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 客户迁移历史分页列表
     */
    @Override
    public TableDataInfo<HmCustomerTransferHistoryVo> queryPageList(HmCustomerTransferHistoryBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HmCustomerTransferHistory> lqw = buildQueryWrapper(bo);
        Page<HmCustomerTransferHistoryVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的客户迁移历史列表
     *
     * @param bo 查询条件
     * @return 客户迁移历史列表
     */
    @Override
    public List<HmCustomerTransferHistoryVo> queryList(HmCustomerTransferHistoryBo bo) {
        LambdaQueryWrapper<HmCustomerTransferHistory> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HmCustomerTransferHistory> buildQueryWrapper(HmCustomerTransferHistoryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HmCustomerTransferHistory> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCustomerId() != null, HmCustomerTransferHistory::getCustomerId, bo.getCustomerId());
        lqw.like(StringUtils.isNotBlank(bo.getCustomerName()), HmCustomerTransferHistory::getCustomerName, bo.getCustomerName());
        lqw.like(StringUtils.isNotBlank(bo.getOldStaffName()), HmCustomerTransferHistory::getOldStaffName, bo.getOldStaffName());
        lqw.like(StringUtils.isNotBlank(bo.getNewStaffName()), HmCustomerTransferHistory::getNewStaffName, bo.getNewStaffName());
        lqw.like(StringUtils.isNotBlank(bo.getOldStoreName()), HmCustomerTransferHistory::getOldStoreName, bo.getOldStoreName());
        lqw.like(StringUtils.isNotBlank(bo.getNewStoreName()), HmCustomerTransferHistory::getNewStoreName, bo.getNewStoreName());
        lqw.eq(StringUtils.isNotBlank(bo.getTransferType()), HmCustomerTransferHistory::getTransferType, bo.getTransferType());
        lqw.like(StringUtils.isNotBlank(bo.getOperatorName()), HmCustomerTransferHistory::getOperatorName, bo.getOperatorName());
        lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
            HmCustomerTransferHistory::getCreateTime, params.get("beginCreateTime"), params.get("endCreateTime"));
        lqw.orderByDesc(HmCustomerTransferHistory::getCreateTime); // Default sort by transfer time
        return lqw;
    }

    /**
     * 新增客户迁移历史
     *
     * @param bo 客户迁移历史
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(HmCustomerTransferHistoryBo bo) {
        HmCustomerTransferHistory add = MapstructUtils.convert(bo, HmCustomerTransferHistory.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setCustomerTransferHistoryId(add.getCustomerTransferHistoryId());
        }
        return flag;
    }

    /**
     * 新增客户迁移历史
     *
     * @param bo 客户迁移历史
     * @return 是否新增成功
     */
    @Override
    public Boolean insert(HmCustomerTransferHistory bo) {

        boolean flag = baseMapper.insert(bo) > 0;
        if (flag) {
            bo.setCustomerTransferHistoryId(bo.getCustomerTransferHistoryId());
        }
        return flag;
    }

    /**
     * 修改客户迁移历史
     *
     * @param bo 客户迁移历史
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(HmCustomerTransferHistoryBo bo) {
        HmCustomerTransferHistory update = MapstructUtils.convert(bo, HmCustomerTransferHistory.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HmCustomerTransferHistory entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除客户迁移历史信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public void batchInsertHistories(List<HmCustomerTransferHistory> histories) {
        if (histories == null || histories.isEmpty()) {
            return;
        }
        baseMapper.insertBatch(histories);
    }
}
